/**
 * <p>文件名:		Test.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		千方集团CTFO</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.zhbroy.test.groovy;


import groovy.LogDataProcess;
import groovy.lang.GroovyClassLoader;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.caits.lbs.framework.utils.FileHelper;
import com.palmcity.rtti.maintenancemonitor.bean.MaintenanceMonitorException;

/**
 * <p>
 * Test
 * </p>
 * <p>
 * 用途：TODO
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-6-30
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-6-30 下午5:27:16</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-6-30 下午5:27:16</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */

public class InvokeGroovy {
	
	static Logger log = Logger.getLogger(InvokeGroovy.class);
	/**
	 * FIXME 
	 * @param args
	 */
	public static void main(String[] args) {
		testLogData();
	}
	/**
	 * 装载groovy文件 
	 */
	protected static void loadGroovyFile() {
		ClassLoader cl = new InvokeGroovy().getClass().getClassLoader();
		GroovyClassLoader groovyCl = new GroovyClassLoader(cl);
		try {
			// 从文件中读取
			File groovyFile = new File(
					"src/main/groovy/com/zhbroy/groovy/Foo.groovy");
			String str_file = FileHelper.fileToString(groovyFile, "UTF-8");
			String replacement = "println('gpsAmount howto:${gpsAmount}');\r\n return gpsAmount + invalidAmount>100";
			str_file = str_file.replace("ALARM_CONDITION", replacement);
			System.out.println("groovy file :\r\n" + str_file);
			// Class<?> groovyClass = groovyCl.parseClass(groovyFile);
			// 直接使用Groovy字符串,也可以获得正确结果
			Class groovyClass = groovyCl.parseClass(str_file);// 这个返回true

			IFoo foo = (IFoo) groovyClass.newInstance();
			System.out.println("where return:" + foo.where(new Integer(2)));
			System.out
					.println("call " + replacement + " result:" + foo.howto());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 测试日志文件
	 */
	private static void testLogData(){
			 String line_pro="{\"carMemPool\":20000,\"carSpeedMemPool\":20000,\"configure\":\"地图MIF文件：Road_ChengDu.MId,地图索引文件：MapIndexer_ChengDu.txt, 总路段数:73023,路况输出间隔：2.5\",\"dateTime\":1320704540,\"elapseTime\":47,\"firstGpsInfo\":\"CD,18782259712,2011,11,8,6,12,50,104.053,30.5268,0,4,0,1\",\"gpsMemPool\":20000,\"jamAmount\":4,\"logType\":0,\"moduleType\":1,\"outputAmount\":1323,\"pathNodeMemPool\":40000,\"slowAmount\":25,\"smoothAmount\":1294,\"sourceAmount\":1,\"sourceElement\":[{\"failureAmount\":\"207\",\"status\":\"1\",\"totalAmount\":\"1\",\"useAmount\":\"1283\"}],\"startTime\":1320704540,\"text\":\"CD:CD恢复连接！,\",\"vehicleAmount\":612}";
				String line_rece="{\"alarm_report_way\":0,\"alarm_status\":-1,\"alarm_time\":0,\"configure\":\"CityCode=320000\",\"dateTime\":1320735130,\"deal_opid\":0,\"id\":\"\",\"invalidAmount\":1661,\"logType\":1,\"moduleCode\":\"RECEIVE_hb\",\"moduleDesc\":\"xx接收\",\"moduleType\":\"RECEIVE\",\"status\":\"网络连接成功\",\"text\":\"运行正常\",\"totalAmount\":3700,\"totalVehicle\":2165,\"validAmount\":9529}";

				ObjectMapper om = new ObjectMapper();  
				
				try {
					LogDataProcess data_another =  om.readValue(line_pro, LogDataProcess.class);
					
					data_another.checkConditionAlarm();
					log.debug("data_another="+data_another.getText());
				} catch (MaintenanceMonitorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonMappingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
	}
}
